package com.huawei.domain;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.huawei.entity.Users;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true) //忽略未知属性
public class LoginUser implements UserDetails {

    private Users user;

    private List<String> permissions;

    @JSONField(serialize = false)
    List<SimpleGrantedAuthority> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        if(authorities != null)
            return authorities;

        authorities=new ArrayList<>();

        for (String permission : permissions) {
            SimpleGrantedAuthority sga=new SimpleGrantedAuthority(permission);
            authorities.add(sga);
        }
        return authorities;//返回权限信息 没有就返回null
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;//用户没有过期
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;//用户没有被锁定
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;//有没有过期
    }

    @Override
    public boolean isEnabled() {
        return true; //用户能否被使用
    }
}

